package com.zhuhanpeng.cloud.api.control;

import com.zhuhanpeng.cloud.api.common.Response;
import com.zhuhanpeng.cloud.api.entity.ApiUserEntity;
import com.zhuhanpeng.cloud.api.entity.SearchApiUserEntity;
import com.zhuhanpeng.cloud.api.entity.registerEntity;
import com.zhuhanpeng.cloud.api.service.ApiService;
import com.zhuhanpeng.cloud.constant.CloudConstant;
import com.zhuhanpeng.cloud.exception.EmailExitException;
import com.zhuhanpeng.cloud.exception.MobileExitException;
import com.zhuhanpeng.cloud.exception.UserNameExitException;
import com.zhuhanpeng.cloud.login.repository.LoginDao;
import com.zhuhanpeng.cloud.mybatis.entity.PtUserEntity;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PagingEntity;
import com.zhuhanpeng.cloud.spring.control.BaseController;
import com.zhuhanpeng.cloud.user.entity.UserEntity;
import com.zhuhanpeng.cloud.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private ApiService apiService;

    @Resource
    private LoginDao loginDao;//通过loginDao层已写过的方法通过uuid来查询整个list表

    @Resource
    private UserService userService;


    @RequestMapping("/confrimEmail")
    @ResponseBody
    public String confrimEmail(@RequestParam("userName") String userName, @RequestParam("email") String email) {
        logger.info("userName:" + userName + "  email:" + email);
        return sucessJson("api.confrim.email.success");
    }

    @RequestMapping("/queryUserInfo")
    public List<PtUserEntity> queryUserInfo(SearchApiUserEntity entity, PagingEntity pagingEntity) {
        logger.info(pagingEntity);
        return apiService.queryUserInfo(entity, pagingEntity);
    }

    @RequestMapping("/queryUserInfoTotal")
    public String queryUserInfoTotal(SearchApiUserEntity entity) {
        Integer total = apiService.queryUserInfoTotal(entity);
        return totalJson(total);
    }

    @RequestMapping("/queryUser")
    public String queryUserByNameAndPassword(@RequestParam("username") String userName, @RequestParam("password") String password) {
        logger.info("userName" + userName);
        ApiUserEntity apiUserEntity = apiService.queryUserByNameAndPassword(userName, password);
        if (apiUserEntity != null) {
            return successObjJson("login.success", apiUserEntity);
        }
        return failCodeJson("login.fail", 101); //101 behalf fail

    }


    @RequestMapping("/registerAddUser")
    @ResponseBody
    public String registerAddUser(@RequestParam("username") String userName, @RequestParam("password") String password, @RequestParam("phone") String phone) {
        UserEntity entity = new UserEntity();
        entity.setUserName(userName);
        entity.setPassword(password);
        entity.setMobile(phone);
        entity.setEmail("");
        entity.setNiceName("");
        entity.setUserUuid(UUID.randomUUID().toString());
        logger.info(entity);
        try {
            userService.addUser(entity);
        } catch (UserNameExitException e) {
            e.printStackTrace();
            return failCodeJson("user.add.fail", CloudConstant.USER_NAME_EXIT_ERROR);
        } catch (EmailExitException e) {
            e.printStackTrace();
            return failCodeJson("user.add.fail", CloudConstant.USER_EMAIL_EXIT_ERROR);
        } catch (MobileExitException e) {
            e.printStackTrace();
            return failCodeJson("user.add.fail", CloudConstant.USER_MOBILE_EXIT_ERROR);
        }
        return sucessJson("user.add.success");
    }

}

//文件上传下载


